package com.example.lopngoaingu;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;

public class InCorrectWordActivity extends Activity {
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.incorrectword_layout);
		tv = (TextView)findViewById(R.id.tv);
		new GetData(tv).execute();
	}
	
	class GetData extends AsyncTask<Void, Void, Void>{
		TextView tv;
		String s;
		public GetData(TextView tv){
			this.tv = tv;
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			//Load("http://luyenthianhvan.org/2007/06/tm-t-sai-toefl-bi-s-20.html");
			Load("http://luyenthianhvan.org/2007/06/t-ng-ngha-toefl-bi-s-1.html");
			Load("http://luyenthianhvan.org/2008/09/lesson-1-toeic-test.html");
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {		
			super.onPostExecute(result);
			tv.setText(Html.fromHtml(s));
		}
		
		int ConvertChar2Num(String s){
			if(s.equals("A"))
				return 1;
			if(s.equals("B"))
				return 2;
			if(s.equals("C"))
				return 3;
			if(s.equals("D"))
				return 4;
			return 1;
		}
		
		void Load(String address){
			List<Question> data = new ArrayList<Question>(20);
			try{
				Document document = Jsoup.connect(address).get();
				Element postbody = document.select(".post-body").first();
				Element temp = postbody;
						
				for(int i=1;i<=10;i++){
					data.add(new Question());
					Element item = temp.select("#text"+i).first();										
					data.get(i-1).setRight(ConvertChar2Num(item.attr("value")));
					
					Element as1 = temp.select("#C"+i+"A").first();
					Element as2 = temp.select("#C"+i+"B").first();
					Element as3 = temp.select("#C"+i+"C").first();
					Element as4 = temp.select("#C"+i+"D").first();
					data.get(i-1).setAs1(as1.text().replace("(A)", "").trim());
					data.get(i-1).setAs2(as2.text().replace("(B)", "").trim());
					data.get(i-1).setAs3(as3.text().replace("(C)", "").trim());
					data.get(i-1).setAs4(as4.text().replace("(D)", "").trim());										
				}
				
				postbody.select("div[style=clear: both;]").remove();
				postbody.select("p").remove();
				postbody.select("h2").remove();
				postbody.select("input").remove();
				postbody.select("label").remove();
				postbody.select("br").remove();
				
				String content = postbody.toString();
				int start, end;
									
				for(int i=1;i<=10;i++){
					
					start = content.indexOf(i+".");
					if(i!=20){
						int j = i+1;
						end = content.indexOf(j+".");						
						s= content.substring(start, end).replace(i+".", "").trim();
					}else{						
						s = content.substring(start).replace(i+".", "").replace("</div>","").trim();
					}	
					data.get(i-1).setTitle(s);
					
					Log.i("Title",data.get(i-1).getTitle());
					Log.i("Right",""+data.get(i-1).getRight());
					Log.i("AS1",data.get(i-1).getAs1());
					Log.i("AS2",data.get(i-1).getAs2());
					Log.i("AS3",data.get(i-1).getAs3());
					Log.i("AS4",data.get(i-1).getAs4());
				}					
				
				
				
			}catch(Exception e){
				Log.e("Error",e.getMessage());
			}
		}
	}
}
